package com.ruoyi.transaction.service.impl;

import java.util.List;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.entity.TransactionEntity.DTO.SpuSpecDTO;
import com.ruoyi.common.entity.TransactionEntity.SpuSpec;
import com.ruoyi.common.entity.TransactionEntity.VO.SpuVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.transaction.mapper.SpuMapper;
import com.ruoyi.common.entity.TransactionEntity.Spu;
import com.ruoyi.transaction.service.ISpuService;

/**
 * 商品SPUService业务层处理
 * 
 * @author ruoyi
 * @date 2025-08-07
 */
@Service
public class SpuServiceImpl implements ISpuService 
{
    @Autowired
    private SpuMapper spuMapper;

    /**
     * 查询商品SPU
     * 
     * @param id 商品SPU主键
     * @return 商品SPU
     */
    @Override
    public SpuVO selectSpuById(Long id)
    {
        return spuMapper.selectSpuById(id);
    }

    @Override
    public Spu getSpuWithSkuById(Long id) {
        return spuMapper.selectSpuWithSkuById(id);
    }

    /**
     * 查询商品SPU列表
     * 
     *  商品SPU
     * @return 商品SPU
     */
    @Override
    public List<SpuVO> selectSpuList(SpuVO spuVO)
    {
        return spuMapper.selectSpuList(spuVO);
    }

    /**
     * 新增商品SPU
     * 
     * @param spu 商品SPU
     * @return 结果
     */
    @Override

    public Spu insertSpu(Spu spu)
    {
        spu.setCreateTime(DateUtils.getNowDate());
        int result = spuMapper.insertSpu(spu);
        if (result > 0) {
            // 由于XML配置了useGeneratedKeys="true" keyProperty="id"
            // 插入后spu对象的id属性会自动被设置为数据库生成的主键ID
            // 返回包含ID的完整SPU对象
            return spu;
        }
        return null;
    }

    /**
     * 修改商品SPU
     * 
     * @param spu 商品SPU
     * @return 结果
     */
    @Override
    public int updateSpu(Spu spu)
    {
        spu.setUpdateTime(DateUtils.getNowDate());
        return spuMapper.updateSpu(spu);
    }

    /**
     * 批量删除商品SPU
     * 
     * @param ids 需要删除的商品SPU主键
     * @return 结果
     */
    @Override
    public int deleteSpuByIds(Long[] ids)
    {
        return spuMapper.deleteSpuByIds(ids);
    }

    /**
     * 删除商品SPU信息
     * 
     * @param id 商品SPU主键
     * @return 结果
     */
    @Override
    public int deleteSpuById(Long id)
    {
        return spuMapper.deleteSpuById(id);
    }


    @Override
    public List<SpuSpecDTO> getAllSpuWithSpecs() {
        return spuMapper.selectAllSpuWithSpecs();
    }
    @Override
    public int addSpuSpec(Long spuId, Long specId) {
        SpuSpec spuSpec = new SpuSpec();
        spuSpec.setSpuId(spuId);
        spuSpec.setSpecId(specId);
        return spuMapper.insertSpuSpec(spuSpec);
    }
    @Override
    public List<Long> getSpuSpecIds(Long spuId) {
        return spuMapper.selectSpuSpecIds(spuId);
    }

    @Override
    public int deleteSpuSpecs(Long spuId) {
        return spuMapper.deleteSpuSpecs(spuId);
    }
}
